#!/bin/bash

#client(running this script) is ssh-ing to server IP and port
serverIP=127.0.0.19
if test -z "$serverIP"; then
  echo "no IP"
  exit 1
fi
serverport=8822
#setting up a tunnel (tun network interfaces) between client and server
tunClientIP='192.168.244.2'  #aka this one we're on right now
tunServerIP='192.168.244.1'
localtun=3 #aka client tun  eg. tun3
remotetun=5 #aka server tun eg. tun5  (can be same on both, eg. tun5 and tun5)

sudo ssh \
  -o PermitLocalCommand=yes \
  -o LocalCommand="sudo ifconfig \"tun${localtun}\" \"$tunClientIP\" pointopoint \"$tunServerIP\" netmask 255.255.255.0" \
  -o ServerAliveInterval=60 \
  -w ${localtun}:${remotetun} "root@${serverIP}" -p ${serverport} \
  "sudo ifconfig \"tun${remotetun}\" \"$tunServerIP\" pointopoint \"$tunClientIP\" netmask 255.255.255.0 && echo \"Server tun${remotetun} ready, C-c when done\""

#the above will stall/hang, until you C-c
#Also note that if target's inet connection gets lost or its dynamic IP changes, then there'll be stuff still stuck on its side that needs to be pkill-ed before this tun script will work again for that target.

echo "Done here!"


